package com.example.everying.controller.user;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.everying.annotation.WebLog;
import com.example.everying.context.UserContext;
import com.example.everying.model.dto.user.UserBalanceTransactionDetailDto;
import com.example.everying.model.dto.user.TransactionPageDto;
import com.example.everying.model.dto.user.UserBalanceTransactionEntityDto;
import com.example.everying.model.dto.user.UserBalanceTransactionStaticDto;
import com.example.everying.model.entity.user.UserBalanceTransactionEntity;
import com.example.everying.model.vo.user.UserBalanceTransactionVO;
import com.example.everying.service.user.UserBalanceTransactionService;
import com.example.everying.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@Api(tags = "用户余额记录")
@RequestMapping("/user/balance")
public class UserBalanceTransactionController {

    @Autowired
    private UserBalanceTransactionService balanceTransactionService;

    //条件查询带分页
    @ApiOperation(value = "分页查询")
    @PostMapping("/page")
    @WebLog(description = "分页查询用户余额记录")
    public Result<?> list(@RequestBody(required = false) UserBalanceTransactionEntityDto dto) {
        Page<UserBalanceTransactionEntity> pageParam = new Page<>(dto.getPage(), dto.getLimit());
        String userId = UserContext.getUser().getId();
        if (userId.isEmpty()){
            return Result.fail("用户未登录");
        }
        IPage<UserBalanceTransactionEntity> pageModel = balanceTransactionService.selectPage(pageParam, dto);
        return Result.ok(pageModel);
    }
    @ApiOperation(value = "分页查询")
    @PostMapping("/stats")
    @WebLog(description = "分页查询用户余额记录 stats")
    public Result<?> stats(@RequestBody(required = false) UserBalanceTransactionStaticDto dto) {
        Page<UserBalanceTransactionEntity> pageParam = new Page<>(dto.getPage(), dto.getLimit());
        String userId = UserContext.getUser().getId();
        if (userId.isEmpty()){
            return Result.fail("用户未登录");
        }
       UserBalanceTransactionVO vo = balanceTransactionService.stats(pageParam,dto);
        return Result.ok(vo);
    }

    @ApiModelProperty(value = "关联用户名分页查询")
    @PostMapping("/list")
    @WebLog(description = "关联用户名分页查询")
    public Result<?> pageWithUserName(@RequestBody(required = false) TransactionPageDto dto) {

        return balanceTransactionService.selectPageWithUser(dto);

    }

    @ApiOperation(value = "详情查询")
    @PostMapping("/detail")
    @WebLog(description = "详情查询")
    public Result<?> detail(@RequestBody UserBalanceTransactionDetailDto dto) {
        String userId = UserContext.getUser().getId();
        if (userId.isEmpty()){
            return Result.fail("用户未登录");
        }
        return balanceTransactionService.detail(dto);
    }
}
